@extends('layouts.app')

@section('content')
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-8">
                <div class="card">
                    <div class="card-header">
                        <h5 class="card-title mb-0">编辑作业</h5>
                    </div>
                    <div class="card-body">
                        <form action="{{ route('assignments.update', $assignment) }}" method="POST">
                            @csrf
                            @method('PUT')

                            <div class="mb-3">
                                <label for="source" class="form-label">作业来源</label>
                                <select class="form-select @error('source') is-invalid @enderror" id="source"
                                    name="source" required>
                                    <option value="">请选择</option>
                                    <option value="school"
                                        {{ old('source', $assignment->source) == 'school' ? 'selected' : '' }}>学校
                                    </option>
                                    <option value="institution"
                                        {{ old('source', $assignment->source) == 'institution' ? 'selected' : '' }}>机构
                                    </option>
                                </select>
                                @error('source')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="subject" class="form-label">科目</label>
                                <select class="form-select @error('subject') is-invalid @enderror" id="subject"
                                    name="subject" required>
                                    <option value="">请选择</option>
                                    <option value="chinese"
                                        {{ old('subject', $assignment->subject) == 'chinese' ? 'selected' : '' }}>语文
                                    </option>
                                    <option value="math"
                                        {{ old('subject', $assignment->subject) == 'math' ? 'selected' : '' }}>数学
                                    </option>
                                    <option value="english"
                                        {{ old('subject', $assignment->subject) == 'english' ? 'selected' : '' }}>英语
                                    </option>
                                    <option value="science"
                                        {{ old('subject', $assignment->subject) == 'science' ? 'selected' : '' }}>科学
                                    </option>
                                </select>
                                @error('subject')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="type" class="form-label">作业类型</label>
                                <select class="form-select @error('type') is-invalid @enderror" id="type"
                                    name="type" required>
                                    <option value="">请选择</option>
                                    <option value="daily"
                                        {{ old('type', $assignment->type) == 'daily' ? 'selected' : '' }}>
                                        每日作业
                                    </option>
                                    <option value="checkin"
                                        {{ old('type', $assignment->type) == 'checkin' ? 'selected' : '' }}>每日打卡
                                    </option>
                                    <option value="weekly"
                                        {{ old('type', $assignment->type) == 'weekly' ? 'selected' : '' }}>
                                        每周作业
                                    </option>
                                </select>
                                @error('type')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="content" class="form-label">作业内容</label>
                                <textarea class="form-control @error('content') is-invalid @enderror" id="content" name="content" rows="3"
                                    required>{{ old('content', $assignment->content) }}</textarea>
                                @error('content')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="mb-3">
                                <label for="due_date" class="form-label">截止日期</label>
                                <input type="date" class="form-control @error('due_date') is-invalid @enderror"
                                    id="due_date" name="due_date"
                                    value="{{ old('due_date', $assignment->due_date->format('Y-m-d')) }}" required
                                    onclick="this.showPicker()">
                                @error('due_date')
                                    <div class="invalid-feedback">{{ $message }}</div>
                                @enderror
                            </div>

                            <div class="d-grid gap-2">
                                <button type="submit" class="btn btn-primary">更新作业</button>
                            </div>
                        </form>

                        <div class="d-flex gap-2 mt-3">
                            <a href="{{ route('assignments.index') }}" class="btn btn-secondary">返回</a>
                            <form action="{{ route('assignments.destroy', $assignment) }}" method="POST" class="d-inline">
                                @csrf
                                @method('DELETE')
                                <button type="submit" class="btn btn-danger" onclick="return confirm('确定要删除这个作业吗？')">
                                    <i class="fas fa-trash"></i> 删除作业
                                </button>
                            </form>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
@endsection
